package com.xdw.ui_demo.recyclerView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.xdw.ui_demo.R;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        //定义适配器对象
        RecyclerViewAdapter adapter = new RecyclerViewAdapter();
        //获取数据
        adapter.setData(getData());
        //需将布局中的RecyclerView对象的id指定为recyclerview
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        //定义线性⽅式显示数据
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(RecyclerViewActivity.this);
        //设置为位垂直⽅式显示（也可以⽔平⽅式显示）
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置适配器
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(),"click"+position,Toast.LENGTH_SHORT).show();
                System.out.println("tttaaa:");
            }
        });




    }
    private List<Dataitem> getData(){
        List<Dataitem> data = new ArrayList<>();
        for (int i = 0; i < 10; i ++){
            Dataitem dataItem = new Dataitem();
            dataItem.setUrl("image" + i);
            dataItem.setText1("text1:" + i);
            dataItem.setText2("text2:" + i);
            data.add(dataItem);
        }
        return data;
    }


}
